package com.mdxx.xczd.manager;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;
import com.mdxx.xczd.R;
import com.orhanobut.logger.Logger;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 媒体音量控制
 *
 */
public class AudioManagerUtil {	

	private Context context;
	private AudioManager audioManager;
	private static AudioManagerUtil audioManagerUtil ;
	private Vibrator mVibrator = null;
	
	private AudioManagerUtil(Context context){
		this.context = context;
		init();
	}
	
	public static AudioManagerUtil getInstance(Context context){
		if(audioManagerUtil == null){
			synchronized (AudioManagerUtil.class) {
				if(audioManagerUtil == null){
					audioManagerUtil = new AudioManagerUtil(context);
				}
			}
		}
		return audioManagerUtil;
	}	
	
	private void init(){
		audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		setMaxVolume();
		getTTS(audioManager);

	}
	
	public void setMaxVolume() {  
		try {
			if(audioManager != null) {
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
						AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL ,
						audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
						AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,
						audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),
						AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_RING ,
						audioManager.getStreamMaxVolume(AudioManager.STREAM_RING),
						AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_ALARM,
						audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),
						AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,
						audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
						AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(getTTS(audioManager) ,
						audioManager.getStreamMaxVolume(9),
						AudioManager.FLAG_PLAY_SOUND);  //STREAM_TTS = 9
			}
		} catch (Exception e) {
			e.printStackTrace();
		}        
		
	}
	
	public void setMinVolume() {
		try {
			if(audioManager != null) {
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,0,AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_RING,0,AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_ALARM,0,AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,0,AudioManager.FLAG_PLAY_SOUND);
				audioManager.setStreamVolume(getTTS(audioManager),0,AudioManager.FLAG_PLAY_SOUND);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}       
	}
	
	public void teamTouchPlayer(){		
		mVibrator.vibrate(100); 		
		MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer player) {
				player.seekTo(0);
			}
		});
		AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.talkroom_begin);
		try {
			mediaPlayer.setDataSource(file.getFileDescriptor(),
					file.getStartOffset(), file.getLength());
			file.close();
			mediaPlayer.setVolume(1.0f, 1.0f);
			mediaPlayer.prepare();
		} catch (IOException e) {
			mediaPlayer = null;
		}
		if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL
				&& mediaPlayer != null) {
			mediaPlayer.start();
		}
	}

	private int getStreamMinVolume(AudioManager audioManager){
		int index = 0;
		try {
			Class c = audioManager.getClass();
			Method  method = c.getDeclaredMethod("getStreamMinVolume",Integer.TYPE);
			method.setAccessible(true);
			Object obj = method.invoke(audioManager, 9);
			index = (Integer)(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return index;
	}

	private int getTTS(AudioManager audioManager){
		int index = 0;
		try {
			Class c = audioManager.getClass();
			Field field = c.getDeclaredField( "STREAM_TTS" ) ;
			field.setAccessible(true);
			index = (int) field.get(audioManager) ;
		} catch (Exception e) {
			e.printStackTrace();
			Logger.d("e-----"+e.toString());
		}
		return index;
	}
}
